﻿using AgencyManagerSystem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
namespace AgencyManagerSystem.DataFetcher
{
    public class CustPriceFetcher : ErpFetcher<CustPriceModel>
    {
        public CustPriceFetcher() : base()
        {
             
        }
        public CustPriceModel GetByProductionAndCustomer(int prod,string customer,string unit,string date)
        {
            DateTime dt = DateTime.Now;
            DateTime.TryParse(date,out dt);
            if (String.IsNullOrWhiteSpace(date))
                dt = DateTime.Now;
            ProductionModel productionModel = new ProductionFetcher().Get(prod);
            if (productionModel == null)
                return null;
            Condition[] conditions = new Condition[] {
                new Condition() { Key = "MATERIALCODE", Op = "=", Value = productionModel.Code },
                new Condition(){ Key="CustCode",Op="=",Value=customer },
                new Condition(){ Key="Unit",Op="=",Value=unit },
                new Condition(){ Key="FEFFECTIVEDATE",Op="<=",Value=dt.ToString("yyyy-MM-dd 00:00:00.000") },
                new Condition(){ Key="FEXPRIYDATE",Op=">=",Value=dt.ToString("yyyy-MM-dd 00:00:00.000") }
            };
            var list = List(conditions);
            if (list.Count > 0)
                return list.First();
            return null;
        }
    }
}
